home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / futilsrc.zoo / fileutil / src / touch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-27  |  7.4 KB  |  350 lines

  1. /* touch -- change modification and access times of files
  2.    Copyright (C) 1987, 1989-1991 Free Software Foundation Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Options:
  19.    -a, +time={atime,access,use}    Change access time only.
  20.    -c, +no-create        Do not create files that do not exist.
  21.    -d, +date=TIME        Specify time and date in various formats.
  22.    -m, +time={mtime,modify}    Change modification time only.
  23.    -r, +file=FILE        Use the time and date of reference file FILE.
  24.    -t TIME            Specify time and date in the form
  25.                 `MMDDhhmm[[CC]YY][.ss]'.
  26.    
  27.    If no options are given, -am is the default, using the current time.
  28.    The -r, -t, and -d options are mutually exclusive.  If a file does not
  29.    exist, create it unless -c is given.
  30.  
  31.    Written by Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie,
  32.    and Randy Smith. */
  33.  
  34. #include <stdio.h>
  35. #include <ctype.h>
  36. #include <getopt.h>
  37. #include <sys/types.h>
  38. #include "system.h"
  39.  
  40. #ifdef STDC_HEADERS
  41. #include <time.h>
  42. #else
  43. time_t mktime ();
  44. time_t time ();
  45. #endif
  46.  
  47. int argmatch ();
  48. int touch ();
  49. time_t get_date ();
  50. time_t posixtime ();
  51. void error ();
  52. void invalid_arg ();
  53. void usage ();
  54.  
  55. /* Bitmasks for `change_times'. */
  56. #define CH_ATIME 1
  57. #define CH_MTIME 2
  58.  
  59. /* Which timestamps to change. */
  60. int change_times;
  61.  
  62. /* (-c) If nonzero, don't create if not already there. */
  63. int no_create;
  64.  
  65. /* (-d) If nonzero, date supplied on command line in get_date formats. */
  66. int flexible_date;
  67.  
  68. /* (-r) If nonzero, use times from a reference file. */
  69. int use_ref;
  70.  
  71. /* (-t) If nonzero, date supplied on command line in POSIX format. */
  72. int posix_date;
  73.  
  74. /* If nonzero, the only thing we have to do is change both the
  75.    modification and access time to the current time, so we don't
  76.    have to own the file, just be able to read and write it.  */
  77. int amtime_now;
  78.  
  79. /* New time to use when setting time. */
  80. time_t newtime;
  81.  
  82. /* File to use for -r. */
  83. char *ref_file;
  84.  
  85. /* Info about the reference file. */
  86. struct stat ref_stats;
  87.  
  88. /* The name by which this program was run. */
  89. char *program_name;
  90.  
  91. struct option longopts[] =
  92. {
  93.   {"time", 1, 0, 130},
  94.   {"no-create", 0, 0, 'c'},
  95.   {"date", 1, 0, 'd'},
  96.   {"file", 1, 0, 'r'},
  97.   {0, 0, 0, 0}
  98. };
  99.  
  100. /* Valid arguments to the `+time' option. */
  101. char *time_args[] =
  102. {
  103.   "atime", "access", "use", "mtime", "modify", 0
  104. };
  105.  
  106. /* The bits in `change_times' that those arguments set. */
  107. int time_masks[] =
  108. {
  109.   CH_ATIME, CH_ATIME, CH_ATIME, CH_MTIME, CH_MTIME
  110. };
  111.  
  112. void
  113. main (argc, argv)
  114.      int argc;
  115.      char **argv;
  116. {
  117.   int c, i;
  118.   int date_set = 0;
  119.   int err = 0;
  120.  
  121.   program_name = argv[0];
  122.   change_times = no_create = use_ref = posix_date = flexible_date = 0;
  123.   newtime = (time_t) -1;
  124.  
  125.   while ((c = getopt_long (argc, argv, "acd:mr:t:", longopts, (int *) 0))
  126.      != EOF)
  127.     {
  128.       switch (c)
  129.     {
  130.     case 'a':
  131.       change_times |= CH_ATIME;
  132.       break;
  133.  
  134.     case 'c':
  135.       no_create++;
  136.       break;
  137.  
  138.     case 'd':
  139.       flexible_date++;
  140.       newtime = get_date (optarg, NULL);
  141.       if (newtime == (time_t) -1)
  142.         error (1, 0, "invalid date format `%s'", optarg);
  143.       date_set++;
  144.       break;
  145.  
  146.     case 'm':
  147.       change_times |= CH_MTIME;
  148.       break;
  149.  
  150.     case 'r':
  151.       use_ref++;
  152.       ref_file = optarg;
  153.       break;
  154.  
  155.     case 't':
  156.       posix_date++;
  157.       newtime = posixtime (optarg);
  158.       if (newtime == (time_t) -1)
  159.         error (1, 0, "invalid date format `%s'", optarg);
  160.       date_set++;
  161.       break;
  162.  
  163.     case 130:
  164.       i = argmatch (optarg, time_args);
  165.       if (i < 0)
  166.         {
  167.           invalid_arg ("time selector", optarg, i);
  168.           usage ();
  169.         }
  170.       change_times |= time_masks[i];
  171.       break;
  172.  
  173.     default:
  174.       usage ();
  175.     }
  176.     }
  177.  
  178.   if (change_times == 0)
  179.     change_times = CH_ATIME | CH_MTIME;
  180.  
  181.   if ((use_ref && (posix_date || flexible_date))
  182.       || (posix_date && flexible_date))
  183.     {
  184.       error (0, 0, "cannot specify times from more than one source");
  185.       usage ();
  186.     }
  187.  
  188.   if (use_ref)
  189.     {
  190.       if (stat (ref_file, &ref_stats))
  191.     error (1, errno, "%s", ref_file);
  192.       date_set++;
  193.     }
  194.  
  195.   if (!date_set && optind < argc && strcmp (argv[optind - 1], "--"))
  196.     {
  197.       newtime = posixtime (argv[optind]);
  198.       if (newtime != (time_t) -1)
  199.     {
  200.       optind++;
  201.       date_set++;
  202.     }
  203.     }
  204.   if (!date_set)
  205.     {
  206.       if ((change_times & (CH_ATIME | CH_MTIME)) == (CH_ATIME | CH_MTIME))
  207.     amtime_now = 1;
  208.       else
  209.     time (&newtime);
  210.     }
  211.  
  212.   if (optind == argc)
  213.     {
  214.       error (0, 0, "file arguments missing");
  215.       usage ();
  216.     }
  217.  
  218.   for (; optind < argc; ++optind)
  219.     err += touch (argv[optind]);
  220.  
  221.   exit (err != 0);
  222. }
  223.  
  224. /* Update the time of file FILE according to the options given.
  225.    Return 0 if successful, 1 if an error occurs. */
  226.  
  227. int
  228. touch (file)
  229.      char *file;
  230. {
  231.   int status;
  232.   struct stat sbuf;
  233.   int fd;
  234.  
  235.   if (stat (file, &sbuf))
  236.     {
  237.       if (errno != ENOENT)
  238.     {
  239.       error (0, errno, "%s", file);
  240.       return 1;
  241.     }
  242.       if (no_create)
  243.     return 0;
  244.       fd = creat (file, 0666);
  245.       if (fd == -1)
  246.     {
  247.       error (0, errno, "%s", file);
  248.       return 1;
  249.     }
  250.       if (amtime_now)
  251.     {
  252.       if (close (fd) < 0)
  253.         {
  254.           error (0, errno, "%s", file);
  255.           return 1;
  256.         }
  257.       return 0;        /* We've done all we have to. */
  258.     }
  259.       if (fstat (fd, &sbuf))
  260.     {
  261.       error (0, errno, "%s", file);
  262.       close (fd);
  263.       return 1;
  264.     }
  265.       if (close (fd) < 0)
  266.     {
  267.       error (0, errno, "%s", file);
  268.       return 1;
  269.     }    
  270.     }
  271.  
  272.   if (amtime_now)
  273.     {
  274. #if defined (UTIME_NULL_MISSING)
  275.       status = utime_now (file, sbuf.st_size);
  276. #else
  277.       /* Pass NULL to utime so it will not fail if we just have
  278.      write access to the file, but don't own it.  */
  279.       status = utime (file, NULL);
  280. #endif
  281.     }
  282.   else
  283.     {
  284.       struct utimbuf utb;
  285.  
  286.       if (use_ref)
  287.     {
  288.       utb.actime = ref_stats.st_atime;
  289.       utb.modtime = ref_stats.st_mtime;
  290.     }
  291.       else
  292.     utb.actime = utb.modtime = newtime;
  293.  
  294.       if (!(change_times & CH_ATIME))
  295.     utb.actime = sbuf.st_atime;
  296.  
  297.       if (!(change_times & CH_MTIME))
  298.     utb.modtime = sbuf.st_mtime;
  299.  
  300.       status = utime (file, &utb);
  301.     }
  302.   
  303.   if (status)
  304.     {
  305.       error (0, errno, "%s", file);
  306.       return 1;
  307.     }
  308.  
  309.   return 0;
  310. }
  311.  
  312. #if defined (UTIME_NULL_MISSING)
  313. /* Emulate utime (file, NULL) for systems (like 4.3BSD) that do not
  314.    interpret it to set the access and modification times of FILE to
  315.    the current time.  FILESIZE is the correct size of FILE, used to
  316.    make sure empty files are not lengthened to 1 byte.
  317.    Return 0 if successful, -1 if not. */
  318.  
  319. int
  320. utime_now (file, filesize)
  321.      char *file;
  322.      off_t filesize;
  323. {
  324.   int fd;
  325.   char c;
  326.   int status = 0;
  327.  
  328.   fd = open (file, O_RDWR, 0666);
  329.   if (fd < 0
  330.       || read (fd, &c, sizeof (char)) < 0
  331.       || lseek (fd, (off_t) 0, SEEK_SET) < 0
  332.       || write (fd, &c, sizeof (char)) < 0
  333.       || ftruncate (fd, filesize) < 0
  334.       || close (fd) < 0)
  335.     status = -1;
  336.   return status;
  337. }
  338. #endif
  339.  
  340. void
  341. usage ()
  342. {
  343.   fprintf (stderr, "\
  344. Usage: %s [-acm] [-r reference-file] [-t MMDDhhmm[[CC]YY][.ss]]\n\
  345.        [-d time] [+time={atime,access,use,mtime,modify}] [+date=time]\n\
  346.        [+file=reference-file] [+no-create] file...\n",
  347.        program_name);
  348.   exit (1);
  349. }
  350.